package com.bocai.yuweiwangluo.activity;


import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.View.XListView;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.bean.BBSListBean;
import com.bocai.yuweiwangluo.presenter.BBSPresenter;
import com.bocai.yuweiwangluo.viewModel.IBBSListView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 论坛分类（足球、篮球、羽毛球、网球）列表界面
 */
public class FootballTalkActivity extends BaseActivity implements View.OnClickListener,XListView.IXListViewListener,IBBSListView,AdapterView.OnItemClickListener
{
    @ViewInject(R.id.lv_mygame_list)
    XListView lv_mygame_list;
    @ViewInject(R.id.ll_nocotent)
    LinearLayout llnoContent;
    @ViewInject(R.id.ll_refresh)
    LinearLayout llRefresh;
    private String type_id;
    private int page=1;
    private BBSPresenter bbsPresenter;
    public static final int REQUST_OK=-1;
    private boolean isRefresh=false;
    @Override
    protected int contentView()
    {
        return R.layout.activity_mygame;
    }

    @Override
    protected void setData()
    {
        ViewUtils.inject(this);
        type_id=getIntent().getStringExtra("type_id");
        String type=getIntent().getStringExtra("type");
        initToolbar(type);
        mToolbar.inflateMenu(R.menu.menu_talk);

        initEvent();
        getBBSPresenter();
        getData();
    }

    private void getData() {
        bbsPresenter.getBBSList(type_id);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (1000==requestCode){
            if(resultCode==REQUST_OK){
                onRefresh();
                isRefresh=true;
            }
        }
    }

    @Override
    protected void initEvent()
    {
        super.initEvent();
        llRefresh.setOnClickListener(this);
        lv_mygame_list.setPullLoadEnable(true);
        lv_mygame_list.setPullRefreshEnable(true);
        lv_mygame_list.setXListViewListener(this);
        lv_mygame_list.setOnItemClickListener(this);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent(FootballTalkActivity.this, PublishTalkActivity.class);
                startActivityForResult(intent, 1000);
                return true;
            }
        });
    }
    @Override
    public void onClick(View v)
    {
        llRefresh.setVisibility(View.GONE);
        onRefresh();
    }

    @Override
    public int getmyPage() {
        return page;
    }

    public void onLoad()
    {
        lv_mygame_list.stopRefresh();
        lv_mygame_list.stopLoadMore();
        lv_mygame_list.setRefreshTime("");
    }

    @Override
    public XListView getListView() {
        return lv_mygame_list;
    }

    @Override
    public LinearLayout getnoContent() {
        return llnoContent;
    }

    @Override
    public BBSPresenter getBBSPresenter() {
        bbsPresenter=new BBSPresenter(this,this,this);
        return bbsPresenter;
    }

    @Override
    public LinearLayout getllRefresh() {
        return llRefresh;
    }

    @Override
    public void finishActivity() {
        if (isRefresh){
            setResult(REQUST_OK);
        }
        this.finish();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            finishActivity();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    /** 刷新*/
    @Override
    public void onRefresh()
    {
        page=1;
        lv_mygame_list.setPullLoadEnable(true);
        bbsPresenter.getBBSList(type_id);
    }
    /** 加载更多*/
    @Override
    public void onLoadMore()
    {
//        if(bbsPresenter.getData().getTotal()>bbsPresenter.getData().getEach()){
        page++;
        bbsPresenter.getBBSList(type_id);
//        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        BBSListBean.Data data= (BBSListBean.Data) parent.getAdapter().getItem(position);
        Intent intent=new Intent(this,TalkDetailActivity.class);
        intent.putExtra("id",data.getId());
        startActivityForResult(intent,1000);
    }
}
